﻿//系统管理-->产品管理
//Create By:Cui 2014年9月28日10:58:40

//1.0 加载JqueryDatabase
jQuery(function ($) {
    var oTable1 = $('#sample-table-2').dataTable({
        "aoColumns": [
          { "bSortable": false },
          { "bSortable": true },
          { "bSortable": true },
          { "bSortable": true },
          { "bSortable": false },
          { "bSortable": true },
          { "bSortable": false },
        ],
        oLanguage: {
            "sProcessing": "正在加载中......",
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "对不起，查询不到相关数据！",
            "sEmptyTable": "表中无数据存在！",
            "sInfo": "当前显示： _START_ 到 _END_ 条，共 _TOTAL_ 条",
            "sInfoFiltered": "数据表中共为 _MAX_ 条记录",
            "sSearch": "",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上一页",
                "sNext": "下一页",
                "sLast": "末页"
            }
        },
        iDisplayLength:20,
        bLengthChange: false
    });


    $('table th input:checkbox').on('click', function () {
        var that = this;
        $(this).closest('table').find('tr > td:first-child input:checkbox')
        .each(function () {
            this.checked = that.checked;
            $(this).closest('tr').toggleClass('selected');
        });

    });


    $('[data-rel="tooltip"]').tooltip({ placement: tooltip_placement });
    function tooltip_placement(context, source) {
        var $source = $(source);
        var $parent = $source.closest('table')
        var off1 = $parent.offset();
        var w1 = $parent.width();

        var off2 = $source.offset();
        var w2 = $source.width();

        if (parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2)) return 'right';
        return 'left';
    }
})



window.onload = function () {

    //2.0 页面初始化
   
    
    $("td").addClass("tdBorder");


    //2.2 table表头上的样式
    $("#sample-table-2>thead tr>th").attr("style", "font-weight:bold;color:black;text-align:center");


    //2.3 table搜索样式
    $("#sample-table-2_filter label input").attr("placeholder", "搜索(多条件搜索中间用空格隔开)").attr("style", "width:50%");
    $("#sample-table-2_filter label").attr("style", "width:100%");
    $("#sample-table-2_filter").attr("style", "margin-right: -16px;");
    $("#sample-table-2_wrapper .row").first().children().eq(1).attr("style", "margin-top:-12px");
    $("#sample-table-2_wrapper").attr("style", "margin-top:-32px");
    $("#butadd").attr("style", "margin-top:-4px");

    //2.4 增加表单行间距样式
    $("div .modal-body .row:not(#iconr)").attr("style", "margin-top:7px;");
    $("div .modal-body .row .col-sm-3").attr("style", "margin-top:6px;text-align:right");
    $("div#myModal .modal-body .row i:lt(6)").attr("style", "margin-top:9px;text-align:right;");

    $("#form_field_select_4_chosen").attr("style", "width:100%");


    //2.5 按钮样式
    $("button.btn.btn-sm:gt(0)").attr("style", "margin:-5px 0px 0px 4px");

    //2.6 显示页数一行的样式
    $("#sample-table-2").next().find(".col-sm-6").eq(0).attr("style", "margin-left: -12px;margin-top: 5px;");
    $("#sample-table-2").next().find(".col-sm-6").eq(1).attr("style", "margin-left: 12px;margin-top: 0px;");


    //3.0   功能
    //3.1   删除


    //3.2   刷新
    $("#buflush").click(function () {
        history.go(0);
    });
    $("#bgdiv").attr("style", "display:none");
    //3.3   新增
    //3.3.1 初始化模态窗口
    function initForm() {
        $("div#myModal .modal-body .row i:lt(5)").attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
        $("div#myModal .modal-body .row input:not([autocomplete])").val("").removeAttr("style");
        //$("div#myModal .modal-body .row select:lt(1)").val("0").removeAttr("style");

        $("#but_saveadd").removeAttr("style");
        $("#but_savemodify").removeAttr("style").attr("style", "display:none");

    }
    //3.3.2 启动模态窗口
    $("#butadd").click(function () {
        initForm();
        $("#myModal").modal();
    });
    //3.3.3 验证表单
    $("#ProductName").focus(function () {
        $(this).removeAttr("style");
        $(this).parent().next().attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
    });
    $("#ProductName").blur(function () {
        var reg = /^[a-zA-Z\d\_\u2E80-\u9FFF]{2,16}$/;  //长度为2-16的字母、数字、下划线、汉字
        var obj = $(this).val();
        if (!reg.test(obj)) {
            $(this).attr("style", " border-color: red;");
            $(this).parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
        }
    });

    $("#Price").focus(function () {
        $(this).removeAttr("style");
        $(this).parent().next().attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
    });
    $("#Price").blur(function () {
        var reg = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/; //最多两位小数
        var obj = $(this).val();
        if (!reg.test(obj)) {
            $(this).attr("style", " border-color: red;");
            $(this).parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
        }
    });
    //3.3.4 新增保存
    $("#but_saveadd").click(function () {
        var ProductName = $("#ProductName").val();
        var Price = $("#Price").val();
        var Description = $("#Description").val();

        var flag = true;
        if (1 == 1) {
            var reg = /^[a-zA-Z\d\_\u2E80-\u9FFF]{2,16}$/;  //长度为2-16的字母、数字、下划线、汉字
            if (!reg.test(ProductName)) {
                flag = false;
                $("#ProductName").attr("style", " border-color: red;");
                $("#ProductName").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
            }
        }
        
       
        if (1 == 1) {
            var reg = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;  //最多两位小数
            if (!reg.test(Price)) {
                flag = false;
                $("#Price").attr("style", " border-color: red;");
                $("#Price").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
            }
        }
        if (!flag) {
            $.gritter.add({
                title: '消息提醒',
                text: "请重新填写表单之后再次提交！",
                class_name: 'gritter-info gritter-center'
            });
            return;
        }


        $.ajax({
            url: "AddNewProduct",
            dataType: "json",
            Type: "POST",
            data: {
                ProductName: ProductName,
                Price: Price,
                Description: Description
            },
            success: function (data) {
                var json = eval(data);
                if (json.result == "Success") {
                    $("#but_infoclose").click();
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });

                    history.go(0);
                }
                else {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                }
            }
        });
    });
    //3.3.5 删除
    $("#butdel").click(function delCheck() {
        var idString = "";
        $("table tbody input[type='checkbox']:checked").parent().parent().next().each(function () {
            idString += $(this).text() + ","
        });
        idString = idString.substring(0, idString.length - 1);
        if (idString.length == 0) {
            $.gritter.add({
                title: '消息提醒',
                text: '请选择要删除的项',
                class_name: 'gritter-info gritter-center'
            });
        }
        else {
            if (!confirm("确定删除")) { return; }
            $.ajax({
                url: "ToDelete",
                dataType: "Json",
                data: { idString: idString },
                Type: "Post",
                success: function (data) {
                    var json = eval(data);
                    if (json.result == "Success") {
                        $.gritter.add({
                            title: '消息提醒',
                            text: json.message,
                            class_name: 'gritter-info gritter-center'
                        });
                        history.go(0);
                    }
                    else {
                        $.gritter.add({
                            title: '消息提醒',
                            text: json.message,
                            class_name: 'gritter-info gritter-center'
                        });
                    }
                }
            });
        }
    });
    //3.3.6 修改产品信息初始化
   
    //3.3.7 保存修改
    $("#but_savemodify").click(function () {
        var ProductID = $("#idHidden").val();
        var ProductName = $("#ProductName").val();
        var Price = $("#Price").val();
        var Description = $("#Description").val();

        var flag = true;
        if (1 == 1) {
            var reg = /^[a-zA-Z\d\_\u2E80-\u9FFF]{2,16}$/;  //长度为2-16的字母、数字、下划线、汉字
            if (!reg.test(ProductName)) {
                flag = false;
                $("#ProductName").attr("style", " border-color: red;");
                $("#ProductName").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
            }
        }
        if (1 == 1) {
            var reg = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/; //最多两位小数
            if (!reg.test(Price)) {
                flag = false;
                $("#Price").attr("style", " border-color: red;");
                $("#Price").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
            }
        }
       
        if (!flag) {
            $.gritter.add({
                title: '消息提醒',
                text: "请重新填写表单之后再次提交！",
                class_name: 'gritter-info gritter-center'
            });
            return;
        }

        $.ajax({
            url: "SaveModifyProduct",
            dataType: "json",
            Type: "POST",
            data: {
                ProductID:ProductID,
                ProductName:ProductName,
                Price:Price,
                Description: Description
            },
            success: function (data) {
                var json = eval(data);
                if (json.result == "Success") {
                    $("#but_infoclose").click();
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                    setTimeout(history.go(0), 1000);
                }
                else {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                }
            }
        });
    });
}
function initForm() {
    $("div#myModal .modal-body .row i:lt(5)").attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
    $("div#myModal .modal-body .row input:not([autocomplete])").val("").removeAttr("style");
    //$("div#myModal .modal-body .row select:lt(1)").val("0").removeAttr("style");

    $("#but_saveadd").removeAttr("style");
    $("#but_savemodify").removeAttr("style").attr("style", "display:none");

}

$("#sample-table-2 i.icon-pencil").click(function () {
    var id = $(this).parent().parent().parent().parent().children().eq(1).text()
    initForm();
    $.ajax({
        url: "LoadProductModify",
        datatype: "json",
        type: "post",
        data: {
            id: id
        },
        success: function (data) {

            var json = eval(data);
            $("#idHidden").val(json.ProductID);
            $("#Price").val(json.Price);
            $("#ProductName").val(json.ProductName);
            $("#Description").val(json.Description);

            $("#but_saveadd").attr("style", "display:none");
            $("#but_savemodify").removeAttr("style");
            $("#myModal").modal();
        }

    });
});